package org.judking.CarKeeper.command.EarlyWarning;

import org.judking.CarKeeper.command.BaseObdCommand;
import org.judking.CarKeeper.service.StcActivityService;
import org.judking.CarKeeper.service.VoiceService.VoiceType;

public class CoolantTemperatureDetector implements WarningDetector {
	private double lowerBound;
	private double upperBound;
	private CoolantStatus status;
	private double curCoolantTemperature;
	private Counter counter;
	
	public CoolantTemperatureDetector()			{
		this.lowerBound = 0;
		this.upperBound = 95;
		this.status = CoolantStatus.LOW;
		this.counter = new Counter(25, true);
	}
	
	@Override
	public synchronized void detect(BaseObdCommand command) {
		curCoolantTemperature = Double.parseDouble(command.getRipeData().toString());
		status = curCoolantTemperature <= lowerBound ? CoolantStatus.LOW : 
					(curCoolantTemperature < upperBound ? CoolantStatus.NORMAL : CoolantStatus.HIGH);
		if(status == CoolantStatus.NORMAL)		{
			counter.reset();
		}
		else if(status == CoolantStatus.LOW)		{
			if(counter.tick())		{
				StcActivityService.mkVoice(VoiceType.COOLANT_LOW);
			}
		}
		else if(status == CoolantStatus.HIGH)		{
			if(counter.tick())		{
				StcActivityService.mkVoice(VoiceType.COOLANT_HIGH);
			}
		}
	}

	public double getLowerBound() {
		return lowerBound;
	}

	public void setLowerBound(double lowerBound) {
		this.lowerBound = lowerBound;
	}

	public double getUpperBound() {
		return upperBound;
	}

	public void setUpperBound(double upperBound) {
		this.upperBound = upperBound;
	}

	public CoolantStatus getStatus() {
		return status;
	}

	public double getCurCoolantTemperature() {
		return curCoolantTemperature;
	}

	static enum CoolantStatus			{
		LOW,
		NORMAL,
		HIGH
	}
}
